package actions;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import db.dao.Dept;
import db.dao.DeptDAO;
import db.dao.Major;
import db.dao.MajorDAO;
/*添加专业*/
public class AddMajorAction extends ActionSupport{
	private String mid;	//专业编号
	private String mname;	//专业名称
	private String did;	//所属学院编号
	
	public AddMajorAction() {
	}

	public String getMid() {
		return mid;
	}

	public void setMid(String mid) {
		this.mid = mid;
	}

	public String getMname() {
		return mname;
	}

	public void setMname(String mname) {
		this.mname = mname;
	}

	public String getDid() {
		return did;
	}

	public void setDid(String did) {
		this.did = did;
	}
	
	public String execute() throws IOException {
		//获取页面信息，用于弹窗提示和清除页面缓存
		HttpServletResponse response = ServletActionContext.getResponse();
		String result="";
		//查找所属学院
		DeptDAO dDao = new DeptDAO();
		Dept dept = dDao.findById(did);
		//设置专业参数
		MajorDAO mDao = new MajorDAO();
		Major major = new Major();
		major.setMajorid(mid);
		major.setMajorname(mname);
		major.setDept(dept);
		//保存
		result = mDao.save(major);
		if (result.equals("success")) {
			//成功弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('添加成功!')</script>");
            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}else {
			//失败弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('添加失败!')</script>");
            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}
	}
}
